package com.spring.boot.standard.admin.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.spring.boot.standard.admin.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.spring.boot.standard.admin.common.utils.GsonUtil;
import com.spring.boot.standard.admin.dao.DeveloperconfigurationRepository;
import com.spring.boot.standard.admin.entity.Developerconfiguration;
import com.spring.boot.standard.admin.service.DeveloperconfigurationService;

/**
 * @author qinhao
 * @since Mon Sep 06 10:36:14 UTC 2021
 */
@Service
@EnableTransactionManagement
public class DeveloperconfigurationServiceImpl implements DeveloperconfigurationService {
	
    @Autowired
    private DeveloperconfigurationRepository developerconfigurationRepository;

    @SuppressWarnings("serial")
	@Override
	public Page<Developerconfiguration> findAll(Pageable pageable,Object[] args) {
		Page<Developerconfiguration> result = developerconfigurationRepository.findAll(new Specification<Developerconfiguration>() {
	        @Override
	        public Predicate toPredicate(Root<Developerconfiguration> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//List<Predicate> list = new ArrayList<Predicate>(); 多条件搜索的写法
				if (!StringUtils.isEmpty(args[0]) && !StringUtils.isEmpty(args[1])) {
					/**多条件搜索的写法
					list.add(cb.like(root.get("developerconfigurationid").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("servercodelist").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("clientcodelist").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("mysqlhost").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("mysqluser").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("mysqlpassword").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("mysqlport").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("mysqldatabase").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("userid").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("projectname").as(String.class), "%" + args[0] + "%"));
					Predicate[] p = new Predicate[list.size()];
					return cb.or(list.toArray(p));*/
					return cb.like(root.get(args[0].toString()).as(String.class),"%" + args[1] + "%");
				}else{
					return null;
				}
	        }
	    }, pageable);
	    return result;
	}

	@Override
	public List<Developerconfiguration> selectListByUserid(String userid){
		Developerconfiguration developerconfiguration = new Developerconfiguration();
		developerconfiguration.setUserid(userid);
		Example<Developerconfiguration> example = Example.of(developerconfiguration);
		try {
			return developerconfigurationRepository.findAll(example);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Developerconfiguration findById(String developerconfigurationid) {
		return developerconfigurationRepository.findById(developerconfigurationid).get();
	}

	@Override
	public Developerconfiguration findByUseridAndProject(String userid, String project) {
		Developerconfiguration developerconfiguration = new Developerconfiguration();
		developerconfiguration.setUserid(userid);
		developerconfiguration.setProjectname(project);
		Example<Developerconfiguration> example = Example.of(developerconfiguration);
		try {
			return developerconfigurationRepository.findOne(example).get();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void deleteById(String developerconfigurationid) {
		developerconfigurationRepository.deleteById(developerconfigurationid);;
	}

	@Override
	public Developerconfiguration save(Developerconfiguration developerconfiguration) {
		return developerconfigurationRepository.save(developerconfiguration);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int saveFromList(List<Map<String,String>> list) throws Exception {
		int i=0;
		List<Developerconfiguration> developerconfigurationlist = new ArrayList<Developerconfiguration>();
		for(Map<String,String> map:list){
			Developerconfiguration developerconfiguration = GsonUtil.getGson().fromJson(GsonUtil.getGson().toJson(map),Developerconfiguration.class);
			developerconfigurationlist.add(developerconfiguration);
		}
		for(Developerconfiguration developerconfiguration:developerconfigurationlist){
			developerconfigurationRepository.save(developerconfiguration);
			i++;
		}
		return i;
	}

    
}
